package com.harry.hr.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.harry.hr.dao.UserDAO;
import com.harry.hr.entity.system.User;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {


	private static final long serialVersionUID = 1L;

	
	public void save(User user) {
		
		try{
			
			this.getHibernateTemplate().save(user);
			
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}


	

	public User update(User user) {
		
		// TODO Auto-generated method stub
		try{
			this.getHibernateTemplate().update(user);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return user;
	}

	public void del(int id) {
		// TODO Auto-generated method stub
		try{
			this.getHibernateTemplate().delete(findByid(id));
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}


	@SuppressWarnings("unchecked")
	public List<User> findAll() {
		// TODO Auto-generated method stub
		String hql="from User";
		return this.getHibernateTemplate().find(hql);
		 
	}



	
	@SuppressWarnings("unchecked")
	public List<User> query(User user,MatchMode mode,Order order) {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		if(user!=null){
			if(user.getName()!=null&&user.getName().length()!=0)
				criteria.add(Restrictions.like("name", user.getName(),mode));
			if(user.getTrueName()!=null&&user.getTrueName().length()!=0)
				criteria.add(Restrictions.like("trueName", user.getTrueName(),mode));
			if(user.getPassword()!=null&&user.getPassword().length()!=0)
				criteria.add(Restrictions.like("password", user.getPassword(),mode));
			if(user.getId()>0)
				criteria.add(Restrictions.idEq(user.getId()));
			if(user.getStartTime()!=null)
				criteria.add(Restrictions.between("startTime", user.getStartTime(), new Date()));
		}
		if(order!=null)
		criteria.addOrder(order);
		return this.getHibernateTemplate().findByCriteria(criteria);
	}




	public User findByid(int id) {
		// TODO Auto-generated method stub
		//String hql="from User u where u.id=?";
		return (User)this.getHibernateTemplate().get(User.class, id);
		 
	}




	public List<User> Search(String name) {
		// TODO Auto-generated method stub
		String hql="from User u where u.name=?";
		return this.getHibernateTemplate().find(hql,name);
	}

}
